package org.openea.eap.module.obpm.controller.custchart;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.openbpm.base.api.aop.annotion.CatchErr;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.base.db.model.table.Column;
import org.openbpm.base.db.model.table.Table;
import org.openbpm.base.rest.BaseController;
import org.openbpm.base.rest.util.RequestUtil;
import org.openea.eap.module.obpm.model.custchart.FormCustChart;
import org.openea.eap.module.obpm.service.custchart.FormCustChartManager;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping({"/form/custChart/"})
public class FormCustChartController extends BaseController<FormCustChart> {

    @Resource
    private FormCustChartManager formCustChartManager;

    @Override
    protected String getModelDesc() {
        return "cust chart";
    }

    @RequestMapping({"getObject"})
    @CatchErr(write2response = true, value = "获取FormCustChart异常")
    public ResultMsg<FormCustChart> getObject(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = RequestUtil.getString(request, "id");
        String key = RequestUtil.getString(request, "key");
        FormCustChart formCustChart = null;
        if (StringUtil.isNotEmpty(id)) {
            formCustChart = this.formCustChartManager.getById(id);
        } else if (StringUtil.isNotEmpty(key)) {
            formCustChart = this.formCustChartManager.getOne(new QueryWrapper<FormCustChart>().lambda().eq(FormCustChart::getKey, key));
        }

        return getSuccessResult(formCustChart);
    }


    @RequestMapping("getJson")
    @CatchErr(write2response = true)
    public ResultMsg<FormCustChart> getJson(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = RequestUtil.getString(request, "id");
        FormCustChart formCustChart = formCustChartManager.getById(id);
        return getSuccessResult(formCustChart);
    }

    @RequestMapping({"searchObjName"})
    @CatchErr(write2response = true, value = "根据数据源获取objName信息失败")
    public ResultMsg<Map<String, String>> searchObjName(HttpServletRequest request, HttpServletResponse response, @RequestBody FormCustChart formCustChart) throws Exception {
        return getSuccessResult(this.formCustChartManager.searchObjName(formCustChart), "根据数据源获取objName信息成功");
    }

    @RequestMapping({"getTable"})
    @CatchErr(write2response = true, value = "根据数据源获取objName的字段信息失败")
    public ResultMsg<Table<Column>> getTable(HttpServletRequest request, HttpServletResponse response, @RequestBody FormCustChart formCustChart) throws Exception {
        return getSuccessResult(this.formCustChartManager.getTable(formCustChart), "根据数据源获取objName的字段信息成功");
    }


    @RequestMapping("checkSql")
    @CatchErr(write2response = true)
    public ResultMsg<String> checkSql(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String dsName = RequestUtil.getString(request, "dsName");
        String sql = RequestUtil.getString(request, "sql");

        // TODO
        return getSuccessResult("OK");
    }


}
